package com.woniuxy.controller;

import com.woniuxy.entity.DrugItem;
import com.woniuxy.entity.DrugPriceAndNum;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.service.DrugItemService;
import com.woniuxy.service.DrugService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class DrugItemController {

    @Autowired
    DrugItemService drugItemService;
    @Autowired
    DrugService drugService;

    @GetMapping("/selectDrugPriceAndNumByNo/{prescriptionNo}")
    public ResponseResult selectDrugPriceAndNumByNo(@PathVariable String prescriptionNo){
        List<DrugPriceAndNum> drugPriceAndNums = drugItemService.selectDrugPriceAndNumByNo(prescriptionNo);

        //通过药品id查询药品名
        for (DrugPriceAndNum drugPriceAndNum:drugPriceAndNums){
            Integer drugId = drugPriceAndNum.getDrugId();
            String drugName = drugService.selectDrugNameById(drugId).getDrugName();
            drugPriceAndNum.setDrugName(drugName);
        }

        return new ResponseResult(200,"查询成功",drugPriceAndNums);
    }

    /*
    * 删除药品条目
    * */
    @PostMapping("/deleteDrugItems")
    public ResponseResult deleteDrugItems(@RequestBody DrugItem drugItem){
        drugItemService.deleteDrugItems(drugItem);
        return new ResponseResult(200,"success",null);
    }
    /*
     * 添加药品条目
     * */
    @PostMapping("/addDrugItems")
    public ResponseResult addDrugItems(@RequestBody DrugItem drugItem ){
        drugItemService.addDrugItems(drugItem);
        return new ResponseResult(200,"success",null);
    }
    /*
    * 按处方单号查询药品处方
    * */
    @GetMapping("/getDrugItems/{pageIndex}/{pageSize}/{prescriptionNo}")
    public ResponseResult getDrugItems(
            @PathVariable String prescriptionNo,
            @PathVariable int  pageIndex,
            @PathVariable int pageSize ){
        log.debug("进入getDrugItems");
        log.debug("查询的prescriptionNo是"+prescriptionNo);
        PageBean<DrugItem> pageBean = drugItemService.getAllDrugItemByPrescriptionNo(prescriptionNo, pageIndex, pageSize);
        return new ResponseResult(200,"success",pageBean);
    }
}
